package com.iotechn.unimall.admin.api.biaozhun;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.ShopBiaozhunDO;
import com.iotechn.unimall.data.domain.SkuDO;
import com.iotechn.unimall.data.dto.goods.SpuDTO;
import com.iotechn.unimall.data.mapper.ShopBiaozhunMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class AdminBiaozhunServiceImpl implements AdminBiaozhunService {
    @Autowired
    private ShopBiaozhunMapper shopBiaozhunMapper;

    public List<ShopBiaozhunDO> treeList(List<ShopBiaozhunDO> list, String parentId) {
        List<ShopBiaozhunDO> treeListResult = new ArrayList<>();
        for (ShopBiaozhunDO each : list) {
            Float allstock = 0.00f;
            Float today = 0.00f;
            if (parentId.equals(each.getGongchengPxuhao())) {
                each.setShopBiaozhunDOSSon(treeList(list,each.getGongchengXuhao()));
                treeListResult.add(each);
            }

        }
        return treeListResult;
    }
    @Override
    public Page<ShopBiaozhunDO> list(Long  shopId,Long  jiedianId,String title,Integer page, Integer limit,  Long adminId) throws ServiceException {
        Wrapper<ShopBiaozhunDO> wrapper = new EntityWrapper<>();
        if(shopId!=null){
            wrapper.eq("shop_id",shopId);
        }
        wrapper.orderBy("id", false);
         List<ShopBiaozhunDO> ShopBiaozhunDOS = shopBiaozhunMapper.selectPage(new RowBounds((page - 1) * 10000, 10000), wrapper);
         List<ShopBiaozhunDO> ShopBiaozhunDOSnew= treeList(ShopBiaozhunDOS, "0");
        return new Page<>(ShopBiaozhunDOSnew, page, limit, ShopBiaozhunDOS.size());
    }

    public Page<ShopBiaozhunDO> listOld(Long  shopId,Long  jiedianId,String title,Integer page, Integer limit,  Long adminId) throws ServiceException {
        Wrapper<ShopBiaozhunDO> wrapper = new EntityWrapper<>();
        if(shopId!=null){
            wrapper.eq("shop_id",shopId);
        }
        if(jiedianId!=null){
            wrapper.eq("jiedian_id",jiedianId);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("gongcheng_name",title);
        }
        wrapper.eq("gongcheng_pxuhao",0);

        wrapper.orderBy("id", false);
        Integer count = shopBiaozhunMapper.selectCount(wrapper);
        List<ShopBiaozhunDO> ShopBiaozhunDOS = shopBiaozhunMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);

        ShopBiaozhunDOS.forEach(item -> {

            List<ShopBiaozhunDO> skuDOList = shopBiaozhunMapper.selectList(new EntityWrapper<ShopBiaozhunDO>().eq("gongcheng_pxuhao", Integer.parseInt(item.getGongchengXuhao())).eq("shop_id",shopId));
            Float allstock = 0.00f;
            Float today = 0.00f;

            if (skuDOList != null && skuDOList.size() > 0) {
                for (int i = 0; i < skuDOList.size(); i++) {
                    ShopBiaozhunDO sku = skuDOList.get(i);
                    if (sku == null) continue;
                    if (sku.getFinishValue() != null) {
                        if(sku.getTodayFinish()!=null) {
                            sku.setFinishValue(sku.getFinishValue() + sku.getTodayFinish());
                        }
                    }else{
                        if(sku.getTodayFinish()!=null){
                            sku.setFinishValue(sku.getTodayFinish());
                        }else{
                            sku.setFinishValue(0f);
                        }
                    }
                    allstock += sku.getFinishValue()*sku.getGongcheng_price();
                    //today+=sku.getTodayFinish();

                }
                item.setFinishValue(allstock);
                //item.setTodayFinish(today);
                item.setShopBiaozhunDOSSon(skuDOList);
            }
        });
        return new Page<>(ShopBiaozhunDOS, page, limit, count);
    }
    @Override
    public List<ShopBiaozhunDO> getBiaozhunByShopId(Long shopId,Long jiedianId) throws ServiceException {
         List<ShopBiaozhunDO> ShopBiaozhunDOS = shopBiaozhunMapper.getBiaozhunByShopId(shopId,jiedianId);
        ShopBiaozhunDOS.forEach(item -> {

            List<ShopBiaozhunDO> skuDOList = shopBiaozhunMapper.selectList(new EntityWrapper<ShopBiaozhunDO>().eq("gongcheng_pxuhao", item.getGongchengXuhao()).eq("shop_id",shopId));
            Float allstock = 0.00f;
            Float today = 0.00f;

            if (skuDOList != null && skuDOList.size() > 0) {
                for (int i = 0; i < skuDOList.size(); i++) {
                    ShopBiaozhunDO sku = skuDOList.get(i);
                    if (sku == null) continue;
                    if (sku.getFinishValue() != null) {
                        if(sku.getTodayFinish()!=null) {
                            sku.setFinishValue(sku.getFinishValue() + sku.getTodayFinish());
                        }
                    }else{
                        if(sku.getTodayFinish()!=null){
                            sku.setFinishValue(sku.getTodayFinish());
                        }else{
                            sku.setFinishValue(0f);
                        }
                    }
                    allstock += sku.getFinishValue()*(sku.getGongcheng_price()==null?0:sku.getGongcheng_price());
                    //today+=sku.getTodayFinish();

                }
                item.setFinishValue(allstock);
                //item.setTodayFinish(today);
                item.setShopBiaozhunDOSSon(skuDOList);
            }
        });
         return ShopBiaozhunDOS;
    }
    @Override
    public List<ShopBiaozhunDO> getBiaozhunByPId(Long shopId,String gongchengPxuhao)  {
        Wrapper<ShopBiaozhunDO> wrapper = new EntityWrapper<ShopBiaozhunDO>();
        if(gongchengPxuhao!=null){
            wrapper.eq("gongcheng_pxuhao",gongchengPxuhao);
        }
        if(shopId!=null){
            wrapper.eq("shop_id",shopId);
        }
        List<ShopBiaozhunDO> ShopBiaozhunDOS = shopBiaozhunMapper.selectList(wrapper);

        return ShopBiaozhunDOS;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopBiaozhunDO create(ShopBiaozhunDO ShopBiaozhunDO, Long adminId) throws ServiceException {
        ShopBiaozhunDO.setAdminId(adminId);
        Date now = new Date();
        ShopBiaozhunDO.setGmtCreate(now);
        ShopBiaozhunDO.setGmtUpdate(now);
        if(ShopBiaozhunDO.getFileList()!=null){
            Gson gson=new Gson();
            ShopBiaozhunDO.setFile(gson.toJson(ShopBiaozhunDO.getFileList()));
        }
        if (shopBiaozhunMapper.insert(ShopBiaozhunDO) > 0) {
            ShopBiaozhunDO.setId(ShopBiaozhunDO.getId());
            return ShopBiaozhunDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(ShopBiaozhunDO ShopBiaozhunDO, Long adminId) throws ServiceException {
        Long id = ShopBiaozhunDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        if(ShopBiaozhunDO.getFileList()!=null && ShopBiaozhunDO.getFileList().size()>0){
            Gson gson=new Gson();
            ShopBiaozhunDO.setFile(gson.toJson(ShopBiaozhunDO.getFileList()));
        }
        ShopBiaozhunDO.setGmtUpdate(now);
        if (shopBiaozhunMapper.updateById(ShopBiaozhunDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (shopBiaozhunMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            shopBiaozhunMapper.deleteById(spuId);
        }
        return "ok";
    }


    @Override
    public ShopBiaozhunDO detail(Long id, Long adminId) throws ServiceException {
        return shopBiaozhunMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        ShopBiaozhunDO ShopBiaozhunDO = new ShopBiaozhunDO();
        ShopBiaozhunDO.setId(id);
        ShopBiaozhunDO.setGmtUpdate(new Date());
        if (shopBiaozhunMapper.updateById(ShopBiaozhunDO) > 0) {
            return true;
        }
        return false;
    }

}